package com.dqhl.community.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.dqhl.community.R;
import com.dqhl.community.base.BaseActivity;
import com.dqhl.community.model.Goods;
import com.dqhl.community.util.Config;
import com.dqhl.community.util.Constant;
import com.dqhl.community.util.Dlog;
import com.dqhl.community.util.JsonUtils;
import com.dqhl.community.util.ScreenUtils;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;


/**
 * Created by LIENLIN on 2016/9/20.
 * 专题页-详情
 */
public class SpecialPageDetailActivity extends BaseActivity implements View.OnClickListener {

    private ImageView iv_topBack;
    private ImageView iv_topOperation;
    private TextView tv_topTitle;
    private TextView mTvAccount;//结算
    private TextView mTvOpen;//展开更多
    private TextView mTvInfo;//展开详情
    private ScrollView mScrollView;//展开详情
    private LinearLayout mLinera;//购物车
    private TextView mTvAllMoney;//总金额
    private TextView mTvPrives;//价格
    private TextView mTvTitle;//标题
    private ImageView mIvBanner;//详情图
    private String price;//单价
    private String img;//图片路径
    private String goodsId;//商品id
    private String name;//商品名

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_special_page);

        initView();
        Intent intent = getIntent();
//        price = intent.getStringExtra("price");
//        img = intent.getStringExtra("img");
        goodsId = intent.getStringExtra("goodsId");
//        name = intent.getStringExtra("name");
//        mTvPrives.setText("¥"+price);
//        mTvAllMoney.setText("¥"+price);
//        mTvTitle.setText(name);

        getData(goodsId);
    }

    private void initView() {
        tv_topTitle = (TextView) findViewById(R.id.tv_top_title);
        mTvAccount = (TextView) findViewById(R.id.tv_account);
        iv_topBack = (ImageView) findViewById(R.id.iv_top_back);
        iv_topOperation = (ImageView) findViewById(R.id.tv_top_operation);
        mTvOpen = (TextView) findViewById(R.id.tv_special_detail_zhankai);
        mTvInfo = (TextView) findViewById(R.id.tv_special_detail_info);
        mScrollView = (ScrollView) findViewById(R.id.scrollview_specila);
        mLinera = (LinearLayout) findViewById(R.id.ll_add_gouwuche);
        mTvAllMoney = (TextView) findViewById(R.id.tv_shopping_allmoney);
        mTvPrives = (TextView) findViewById(R.id.tv_special_detail_prives);
        mTvTitle = (TextView) findViewById(R.id.tv_bar_title);
        mIvBanner = (ImageView) findViewById(R.id.iv_banner);

        iv_topOperation.setVisibility(View.VISIBLE);
        iv_topBack.setVisibility(View.VISIBLE);
        tv_topTitle.setVisibility(View.GONE);

        //动态设置mScrollView高度
        int height= ScreenUtils.getScreenHeight(this);
        int px=ScreenUtils.dp2px(this, 75);
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) mScrollView.getLayoutParams();
        params.height=height-px;
        mScrollView.setLayoutParams(params);
        iv_topOperation.setImageResource(R.drawable.ic_shera);

        iv_topBack.setOnClickListener(this);
        mTvOpen.setOnClickListener(this);
        mTvAccount.setOnClickListener(this);
        mLinera.setOnClickListener(this);
    }

    int status=0;//0:未展开，1：展开
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.iv_top_back:
                finish();
                break;
            case R.id.tv_special_detail_zhankai:
                if (status==0){
                    mTvInfo.setMaxLines(100);
                    mTvOpen.setText("收起");
                    status=1;
                }else {
                    mTvInfo.setMaxLines(4);
                    mTvOpen.setText("展开");
                    status=0;
                }
                break;
            case R.id.tv_account:
                //TODO 结算
                Bundle bundle = new Bundle();
                bundle.putString("total_money", price);
                bundle.putInt("number", 1);
                bundle.putString("img", img);
                bundle.putString("name", name);
                bundle.putString("goods_id", goodsId);
                bundle.putString("price", price);
                overlay(ShoppingPaymentActivity.class,bundle);
                break;
            case R.id.ll_add_gouwuche:
                //TODO 加购物车
                addCart(goodsId,spUserInfo.getString(Constant.USERID, ""),"1");
                break;
        }
    }

    /**
     * 获取商品详情信息
     * @param goodsid
     */
    private void getData(String goodsid) {
        showCustomLoadBar(R.string.bar_load_data);
        RequestParams params = new RequestParams(Config.Api.supermarketContent);
        params.addBodyParameter("good_id", goodsid);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Dlog.e(result);
                int errCode = JsonUtils.getErrCode(result);
                String data = JsonUtils.getInfo(result, "data");
                if (errCode == 0) {
                    Goods goods = JSON.parseObject(data, Goods.class);
                    price = goods.getPrice();
                    img = goods.getPic_a();
                    name = goods.getName();

                    mTvPrives.setText("¥"+price);
                    mTvAllMoney.setText("¥"+price);
                    mTvTitle.setText(name);
//                    TODO mIvBanner暂时写死了

                } else {
                    toast("服务器异常");
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                toast("网络连接失败");
            }
            @Override
            public void onCancelled(CancelledException cex) {

            }
            @Override
            public void onFinished() {
                hideCustomLoadBar();
            }
        });
    }

    //加购物车
    private void addCart(String goodid,String userid,String num) {
        showCustomLoadBar(R.string.loading);
//        Dlog.e(spUserInfo.getString(Constant.USERID, "") + "GG" + strGoodsId + " *** " + totalNum);
        RequestParams parms = new RequestParams(Config.Api.addShopCart);
        parms.addBodyParameter("good_id", goodid);
        parms.addBodyParameter("userid", userid);
        parms.addBodyParameter("number", num);
        x.http().post(parms, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                int errCode = JsonUtils.getErrCode(result);
                Dlog.e("result=" + result);
                if (errCode == 0) {
                    toast("加入成功！");
                } else {
                    toast("加入失败");
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                toast(R.string.error_net_connection);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                hideCustomLoadBar();
            }
        });
    }
}
